﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;

namespace Publicador
{
    public partial class Autenticar : Form
    {
        public bool Activo = false;

        public Autenticar()
        {
            InitializeComponent();
            Scan scan = new Scan();
            scan.Cargar();
            DialogResult res = scan.ShowDialog();
            if(res != DialogResult.OK)
                System.Environment.Exit(0);
            timer1.Enabled = true;
            /*for (int i = 1; i < 100; i++)
            {
                zxc.Items.Add( ((i < 10) ? "0" + i.ToString() : i.ToString()));   
            }*/
            ToolStripStatusLabel lab = new ToolStripStatusLabel();
            GroupBox g = new GroupBox();
            TextBox asd = new TextBox();
            TextBox qwe = new TextBox();
            E14 e14 = new E14(ref asd, ref qwe, ref lab, ref g);
            foreach (DataRow item in e14.ObtenerMunicipios().Rows)
            {
                txtMunicipio.Items.Add(item["nombrempio"].ToString());
            }            
            //panel2.Enabled = true;
            Activo = false;
        }

        private void butCancelar_Click_1(object sender, EventArgs e)
        {
            //this.Hide();
            //Transmision form = new Transmision();
            //form.MUNICIPIO = "CARTAGENA";
            //form.ShowDialog();
            System.Environment.Exit(0);
        }

        public void Validar()
        {
            if (this.Activo)
            {
                picConnOff.Visible = false;
                picConnOn.Visible = true;
                txtConnEstado.Text = "Validando Version";
                timer1.Enabled = false;
                ToolStripStatusLabel lab = new ToolStripStatusLabel();
                GroupBox g = new GroupBox();
                TextBox asd = new TextBox();
                TextBox qwe = new TextBox();
                E14 e14 = new E14(ref asd, ref qwe, ref lab, ref g);
                if (!e14.ValidaVersion())
                {
                    MessageBox.Show("La Version del aplicativo no es la más reciente.\nPor favor actualize.");
                    System.Environment.Exit(0);
                }
                else
                {
                    txtConnEstado.Text = "Version Correcta";
                    panel2.Enabled = true;
                    //timer1.Enabled = true;
                }
                //butTransmitir.Enabled = true;
                /*
                if (!timTransmitir.Enabled)
                {
                    timTransmitir.Enabled = true;
                    timTransmitir.Interval = 36000;
                }
                */
            }
            else
            {
                picConnOn.Visible = false;
                picConnOff.Visible = true;
                panel2.Enabled = false;
                txtConnEstado.Text = "No Conectado";
                //butTransmitir.Enabled = false;
                //timTransmitir.Enabled = false;
                //timTransmitir.Interval = 3000;
            }
        }

        private void butIngresar_Click_1(object sender, EventArgs e)
        {
            ToolStripStatusLabel lab = new ToolStripStatusLabel();
            GroupBox g = new GroupBox();
            TextBox asd = new TextBox();
            TextBox qwe = new TextBox();
            E14 e14 = new E14(ref asd, ref qwe, ref lab, ref g);
            //if (txtMunicipio.Text == txtPassword.Text)
            bool textIsNumeric = true;
            try
            {
                int.Parse(txtPassword.Text);
            }
            catch
            {
                textIsNumeric = false;
            }
            if (txtMunicipio.SelectedIndex != -1 && textIsNumeric)
            {
                if (qwe.Text.ToUpper() == "TODOS" || e14.Autenticar(txtMunicipio.SelectedItem.ToString().ToUpper()))
                {
                    this.Hide();
                    Transmision form = new Transmision();
                    form.MUNICIPIO = txtMunicipio.SelectedItem.ToString().ToUpper();
                    form.USUARIO = "PC" + ((int.Parse(txtPassword.Text) < 10) ? "0" + txtPassword.Text.ToUpper() : txtPassword.Text.ToUpper());
                    form.ShowDialog();
                }
                else
                {
                    MessageBox.Show("Datos Incorrectos", "Ingreso E14", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    qwe.Text = "";
                    asd.Text = "";
                }
            }            
            else {
                MessageBox.Show("Selecciona Municipio e Ingresa tu numero de PC", "Ingreso E14", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);                
            }            
        }

        private void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                butIngresar_Click_1(null, null);
        }

        private void txtMunicipio_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                butIngresar_Click_1(null, null);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Ping ping = new Ping();
            try
            {
                PingReply pingReply = ping.Send(Properties.Settings.Default.E14IpPing);
                if (pingReply.Status == IPStatus.Success)
                {
                    Activo = true;
                }
                else
                {
                    Activo = false;
                }
            }
            catch (Exception)
            {
                Activo = false;
            }
            Validar();
        }

    }
}
